package com.example.demo.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.annotation.RepeatForm;
import com.example.demo.common.enums.StaticValues;
import com.example.demo.entity.Question;
import com.example.demo.entity.User;
import com.example.demo.mapper.CronMapper;
import com.example.demo.mapper.UserMapper;
import com.example.demo.service.impl.QuestionServiceImpl;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

/**
 * @Author: ak
 * @Date: 2020/3/30 10:48
 */
@Controller
public class IndexController {
    @Autowired
    QuestionServiceImpl questionService;
    @Autowired
    UserMapper userMapper;

    @GetMapping("/")
    public String index(Model model) {
        List<Question> list = questionService.list();
        model.addAttribute("questionList", list);
        return "index";
    }

    @GetMapping("index")
    public String index2(Model model) {
        List<Question> list = questionService.list();
        model.addAttribute("questionList", list);
        return "index";
    }

    @GetMapping("index2")
    public String index23(Model model) {
        List<Question> list = questionService.list();
        model.addAttribute("questionList", list);
        return "index2";
    }

    @GetMapping("forum")
    @RepeatForm
    public String forum(HttpServletRequest request) {
        Page<Question> pageService = questionService.getQuestionList(1, 10);
        request.setAttribute("questionList", pageService.getRecords());
        request.setAttribute("curPage", 1);
        request.setAttribute("nextPage", "");
        return "forum";
    }

    @GetMapping("menu")
    public String menu() {
        return "menu";
    }

    @GetMapping("home")
    public String home(Model model) {
        User user = (User) SecurityUtils.getSubject().getPrincipal();
        if (user == null) {
            return "home";
        }
        if (user.getURole().equals(StaticValues.ADMIN)) {
            model.addAttribute("msg", "欢迎你，管理员" + user.getUUsername());
            model.addAttribute("user", user);
            return "home";
        }
        model.addAttribute("msg", "欢迎你，" + user.getUUsername());
        model.addAttribute("user", user);
        return "home";
    }
}
